<?php

namespace app\model\v1\repair;

use app\model\other\CityArea;
use crmeb\traits\ModelTrait;
use think\facade\Db;
use think\Model;

class Construction extends Model
{

    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];



    /**
     * 装修质保管理列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public static function getConstructionList($param = [])
    {
        $data["data"] = self::field ("*,
            case when status=1 then '启用' 
            when status=2 then '禁用' 
            end as status_text 
            ")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('ctime', 'between', [$wheretime[0],$wheretime[1]]);
                }
                if(!empty($param["word"])) $query->where("name",'like', '%'.$param["word"].'%');
            })
            ->order("id", "desc")
            ->page($param["page"], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::field ("*,
            case when status=1 then '启用' 
            when status=2 then '禁用' 
            end as status_text 
            ")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('ctime', 'between', [$wheretime[0],$wheretime[1]]);
                }
                if(!empty($param["word"])) $query->where("name",'like', '%'.$param["word"].'%');
            })
            ->count();

        return $data;
    }


    /**
     * 装修质保管理列表 - 编辑
     * @param $param
     * @return ConfigBanner
     */
    public static function updateConstruction($param = [])
    {
        $update["update_time"] = time();  //更新时间
        if(!empty($param["brand"]))       $update["brand"] = $param["brand"];
        if(!empty($param["category_id"]))       $update["category_id"] = $param["category_id"];
        if(!empty($param["uid"]))       $update["uid"] = $param["uid"];
        if(!empty($param["name"]))      $update["name"] = $param["name"];
        if(!empty($param["decoration"]))       $update["decoration"] = $param["decoration"];
        if(!empty($param["lease_contract"]))      $update["lease_contract"] = $param["lease_contract"];
        if(!empty($param["property_certificate"]))      $update["property_certificate"] = $param["property_certificate"];
        if(!empty($param["Industry_attributes"]))      $update["Industry_attributes"] = $param["Industry_attributes"];
        if(!empty($param["lease_term"]))            $update["lease_term"] = $param["lease_term"];
        if(!empty($param["decoration_address"]))      $update["decoration_address"] = $param["decoration_address"];
        if(!empty($param["construction_unit"]))      $update["construction_unit"] = $param["construction_unit"];
        if(!empty($param["behalf_party"]))      $update["behalf_party"] = $param["behalf_party"];
        if(!empty($param["behalf_phone"]))      $update["behalf_phone"] = $param["behalf_phone"];
        if(!empty($param["partyb_party"]))      $update["partyb_party"] = $param["partyb_party"];
        if(!empty($param["partyb_phone"]))      $update["partyb_phone"] = $param["partyb_phone"];
        if(!empty($param["construction_foreman"]))      $update["construction_foreman"] = $param["construction_foreman"];
        if(!empty($param["construction_phone"]))      $update["construction_phone"] = $param["construction_phone"];
        if(!empty($param["contract_period"]))      $update["contract_period"] = $param["contract_period"];
        if(!empty($param["construction_delay"]))      $update["construction_delay"] = $param["construction_delay"];
        if(!empty($param["construction_changes"]))      $update["construction_changes"] = $param["construction_changes"];
        if(!empty($param["completion_acceptance_time"]))      $update["completion_acceptance_time"] = $param["completion_acceptance_time"];
        if(!empty($param["remove"]))      $update["remove"] = $param["remove"];
        if(!empty($param["construction"]))      $update["construction"] = $param["construction"];
        if(!empty($param["construction_budget"]))      $update["construction_budget"] = $param["construction_budget"];
        if(!empty($param["disclosure"]))      $update["disclosure"] = $param["disclosure"];
        if(!empty($param["hidden"]))      $update["hidden"] = $param["hidden"];
        if(!empty($param["completion_acceptance"]))      $update["completion_acceptance"] = $param["completion_acceptance"];
        if(!empty($param["engineering_settlement"]))      $update["engineering_settlement"] = $param["engineering_settlement"];
        if(!empty($param["warranty"]))      $update["warranty"] = $param["warranty"];
        if(!empty($param["testimonials"]))      $update["testimonials"] = $param["testimonials"];
        if(!empty($param["design_unit"]))      $update["design_unit"] = $param["design_unit"];
        if(!empty($param["project_design"]))      $update["project_design"] = $param["project_design"];
        if(!empty($param["design_phone"]))      $update["design_phone"] = $param["design_phone"];
        if(!empty($param["working_drawing"]))      $update["working_drawing"] = $param["working_drawing"];
        if(!empty($param["rendering"]))      $update["rendering"] = $param["rendering"];
        if(!empty($param["panorama"]))      $update["panorama"] = $param["panorama"];
        if(!empty($param["digital_img"]))      $update["digital_img"] = $param["digital_img"];
        if(!empty($param["changes"]))      $update["changes"] = $param["changes"];
        if(!empty($param["effect_change"]))      $update["effect_change"] = $param["effect_change"];
        if(!empty($param["panoramic_changes"]))      $update["panoramic_changes"] = $param["panoramic_changes"];
        if(!empty($param["digital_change"]))      $update["digital_change"] = $param["digital_change"];
        if(!empty($param["user_name"]))      $update["user_name"] = $param["user_name"];
        if(!empty($param["construction_delay_end"]))      $update["construction_delay_end"] = $param["construction_delay_end"];
        if(!empty($param["cat"]))      $update["cat"] = $param["cat"];
        if(!empty($param["province"]))      $update["province"] = $param["province"];
        if(!empty($param["city"]))      $update["city"] = $param["city"];
        if(!empty($param["district"]))      $update["district"] = $param["district"];
        if(!empty($param["street"]))      $update["street"] = $param["street"];
        if(!empty($param["address_location_name"]))      $update["address_location_name"] = $param["address_location_name"];
        if(!empty($param["unit_name"]))      $update["unit_name"] = $param["unit_name"];
        if(!empty($param["layer_name"]))      $update["layer_name"] = $param["layer_name"];
        if(!empty($param["detail"]))      $update["detail"] = $param["detail"];

        return self::where("id", $param['id'])->update($update);
    }



    /**
     * 装修质保管理列表 - 添加
     * @param $param
     * @return int|string
     */
    public static function addConstruction($param = [])
    {
        $add = [
            "user_id" => $param["admin_id"],
            "brand" => $param["brand"] ?? '',
            "category_id" => $param["category_id"] ?? '',
            "uid" => $param["uid"] ?? '',
            "name" => $param["name"] ?? '',
            "decoration" => $param["decoration"] ?? '',
            "lease_contract" => $param["lease_contract"] ?? '',
            "property_certificate" => $param["property_certificate"] ?? '',
            "Industry_attributes" => $param["Industry_attributes"] ?? '',
            "lease_term" => $param["lease_term"] ?? '',
            "decoration_address" => $param["decoration_address"] ?? '',
            "construction_unit" => $param["construction_unit"] ?? '',
            "behalf_party" => $param["behalf_party"] ?? '',
            "behalf_phone" => $param["behalf_phone"] ?? '',
            "partyb_party" => $param["partyb_party"] ?? '',
            "partyb_phone" => $param["partyb_phone"] ?? '',
            "construction_foreman" => $param["construction_foreman"] ?? '',
            "construction_phone" => $param["construction_phone"] ?? '',
            "contract_period" => $param["contract_period"] ?? '',
            "construction_delay" => $param["construction_delay"] ?? '',
            "construction_changes" => $param["construction_changes"] ?? '',
            "completion_acceptance_time" => $param["completion_acceptance_time"] ?? '',
            "remove" => $param["remove"] ?? '',
            "construction" => $param["construction"] ?? '',
            "construction_budget" => $param["construction_budget"] ?? '',
            "disclosure" => $param["disclosure"] ?? '',
            "hidden" => $param["hidden"] ?? '',
            "completion_acceptance" => $param["completion_acceptance"] ?? '',
            "engineering_settlement" => $param["engineering_settlement"] ?? '',
            "warranty" => $param["warranty"] ?? '',
            "testimonials" => $param["testimonials"] ?? '',
            "design_unit" => $param["design_unit"] ?? '',
            "project_design" => $param["project_design"] ?? '',
            "design_phone" => $param["design_phone"] ?? '',
            "working_drawing" => $param["working_drawing"] ?? '',
            "rendering" => $param["rendering"] ?? '',
            "panorama" => $param["panorama"] ?? '',
            "digital_img" => $param["digital_img"] ?? '',
            "changes" => $param["changes"] ?? '',
            "effect_change" => $param["effect_change"] ?? '',
            "panoramic_changes" => $param["panoramic_changes"] ?? '',
            "digital_change" => $param["digital_change"] ?? '',
            "user_name" => $param["user_name"] ?? '',
            "construction_delay_end" => $param["construction_delay_end"] ?? '',
            "cat" => $param["cat"] ?? '',
            "province" => $param["province"] ?? '',
            "city" => $param["city"] ?? '',
            "district" => $param["district"] ?? '',
            "street" => $param["street"] ?? '',
            "address_location_name" => $param["address_location_name"] ?? '',
            "unit_name" => $param["unit_name"] ?? '',
            "layer_name" => $param["layer_name"] ?? '',
            "detail" => $param["detail"] ?? '',
            "ctime"   => date("Y-m-d H:i:s"),
        ];
        return self::insert($add);
    }




    /**
     * 装修质保管理列表 - 启用禁用
     * @param $param
     * @return string
     */
    public static function enableConstruction($param = [])
    {
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }



    public static function getCityAreaName($id = '')
    {
        return CityArea::where("id", $id)->value("name") ?? '';
    }


}